/*


The MIT License

Copyright (c) 2007 Mohamed El-Shinnawy & Victor Martin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#import "STSpeaka.h"

@implementation STSpeaka

- (void)applicationDidFinishLaunching:(NSNotification *)notification{

	NSLog(@"Showing preferences view");

	//Sustain a little bit longer the splash screen
	sleep(1);

	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

	_speakaWindow = [[UIWindow alloc] initWithContentRect: [UIHardware fullScreenApplicationContentRect]];
	_speakaView = [[UIView alloc] initWithFrame:[_speakaWindow bounds]];

	_speakaPreferences = [[STSpeakaPreferences alloc] initWithAppController:self];

    [_speakaWindow orderFront:self];
    [_speakaWindow makeKey:self];
    [_speakaWindow setContentView:_speakaView];
    //Register for proximity sensor updates
    [self setProximitySensorEnabled:YES];
}

- (UIView *)speakaView{
    return _speakaView;
}

- (void)proximityStateChanged:(BOOL) state{
	if(state)
	{
		NSLog(@"Proximity: OFF");
		[UIHardware setSpeakerPhoneEnabled: NO];
		[self showAlert:@"iSpeaka" msgBody:@"Proximity: OFF"];
	}
	else
	{
		NSLog(@"Proximity: ON");
		[UIHardware setSpeakerPhoneEnabled: YES];
		[self showAlert:@"iSpeaka" msgBody:@"Proximity: ON"];
	}
	[self setProximitySensorEnabled:YES];
}

- (void) showAlert:(NSString *)title msgBody:(NSString *)msg{
	//Method for showing modal alert boxes
	NSMutableArray *buttons = [[NSMutableArray alloc] init];
	[buttons addObject:[NSString stringWithString:@"OK"]];
	UIAlertSheet *alert = [[UIAlertSheet alloc]
		initWithTitle:title
		buttons:buttons
		defaultButtonIndex:0
		delegate:self
		context:self];
	[alert setBodyText:msg];
	[alert _slideSheetOut:YES];
	[alert setRunsModal:YES];
	[alert popupAlertAnimated:YES];
}

- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int) bnum{
	//Callback for event alertSheet->buttonClicked
	[sheet dismiss];
}

- (void)dealloc{

	[_speakaPreferences dealloc];
	_speakaPreferences=nil;

	[_speakaWindow dealloc];
	_speakaWindow=nil;

	[_speakaView dealloc];
	_speakaView=nil;

    [super dealloc];
}
@end
